#Available tasks 
# buildgem : build the rubygem
# builddeb : build the Debian package
# buildsrc : build the source archive

require 'lib/novedit'
require 'date'

require 'rake/gempackagetask'
require 'rake/clean'

pkg_name = $NAME.downcase

#----------- GEM building ---------------
spec = Gem::Specification.new do |s|
  s.name = pkg_name
  s.version = $VERSION
  s.date = Date.today.to_s
  s.authors = $AUTHORS
  s.email = $EMAIL
  s.summary = $SUMMARY
  s.homepage = $HOMEPAGE
  s.description = $DESCRIPTION
  s.executables = ['novedit']
  s.files = FileList['*/**/*'].exclude("bacasable").exclude("features").exclude("pkg").exclude("test").exclude("tools").exclude("novedit.sh")
  s.required_ruby_version = '>=1.8.6'
  s.add_dependency('gettext')
  s.requirements = ["GTK+ 2.16", "libglade2 for ruby ('sudo apt-get install libglade2-ruby' on Debian based systems)"]
end

Rake::GemPackageTask.new(spec) do |package|
  package.need_zip = true
  package.need_tar = true
end

#gem_task : file task name generated by Rake::GemPackageTask
gem_task = "pkg/" + pkg_name + "-" + $VERSION + ".gem"
task :buildgem => gem_task 

#----------- SRC building ---------------
archive_name = pkg_name + "-" + $VERSION + ".tar.bz2"
src_task = "pkg/" + archive_name

CLEAN.include(File.join('pkg', pkg_name))

task src_task => [:clean] do
  system "mkdir pkg/" + pkg_name
  src_files = ["lib", "bin", "novedit.sh", "INSTALL.txt"]
  src_files.each do |afile|
    system "cp -R " + afile + " pkg/" + pkg_name
  end
  cd "pkg"
  system "rm -rf `find " + pkg_name + " -name .svn`"
  system "tar -cjf " + archive_name + " " + pkg_name
  cd ".."
end

task :buildsrc => src_task 

#---------- DEB building (from gem)------------------------
# This task follows the tutorial found at http://blog.loftninjas.org/2008/10/01/debianizing-ruby-gems/
task :builddebgem => :buildgem do 
  system "gem unpack " + gem_task
  workdir = gem_task[4..-5]
#  cp "/usr/lib/ruby/1.8/setup.rb ", workdir
  system "cp /usr/lib/ruby/1.8/setup.rb #{workdir}"
  mkdir workdir + "/debian"
  File.open(workdir + "/debian/changelog", "w") do |file|
    file << "#{pkg_name} (#$VERSION) unstable; urgency=low

  * Initial release.

 -- #$AUTHORS <#$EMAIL>  " + Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
  end

  File.open(workdir + "/debian/rules", "w") do |file|
    file << "#!/usr/bin/make -f
# copyright 2006 by Esteban Manchado Velazquez

include /usr/share/cdbs/1/rules/simple-patchsys.mk
include /usr/share/cdbs/1/rules/debhelper.mk
# Ruby package with setup.rb
include /usr/share/ruby-pkg-tools/1/class/ruby-setup-rb.mk" 
  end

  File.open(workdir + "/debian/control", "w") do |file|
    file << "Source: #{pkg_name}
Section: editors
Priority: optional
Maintainer: #$AUTHORS
Build-Depends: cdbs, debhelper (>> 5.0.0), ruby-pkg-tools, ruby1.8
Standards-Version: 3.8.0

Package: #{pkg_name}
Section: editors
Architecture: all
Depends: ruby1.8, libglade2-ruby (>> 0.17.0), libgettext-ruby1.8
Homepage: #$HOMEPAGE
Description: #$SUMMARY
" 
  end

  cd workdir
  system "dpkg-buildpackage -rfakeroot"
  cd ".."
  #Cleaning
  system "mv -f #{pkg_name}* pkg/debian"
  system "rm -rf #{pkg_name}-#$VERSION"
end

#---------- DEB building ------------------------
#------------------------------------------------------------
## Deb(ian) Package Tasks ##  
#
#file 'pkg/debian/changelog' do |fich|
#  File.open(fich.name, "w") do |file|
#    file << "#{pkg_name} (#$VERSION) unstable; urgency=low
#
#  * Initial release.
#
# -- #$AUTHORS <#$EMAIL>  " + Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
#  end
#end

## clean Task ##
CLEAN.include(File.join('pkg','debian'))

task :debian_init => [:clean] do
  system "cp -R tools/debian_files pkg/debian"
  system "cp -R lib pkg/debian/usr/lib/ruby/1.8"
  system "cp bin/novedit pkg/debian/usr/bin/"
  system "rm -rf `find pkg/debian/ -name .svn`"
end

file 'pkg/debian/DEBIAN/control' => [:debian_init] do |fich|
  File.open(fich.name, "w") do |file|
    file << "Package: #{pkg_name}
Version: #$VERSION
Priority: optional
Maintainer: #$AUTHORS
Section: editors
Architecture: all
Depends: ruby1.8, libglade2-ruby (>> 0.17.0), libgettext-ruby1.8
Homepage: #$HOMEPAGE
Description: #$SUMMARY
" 
  end
end

task :builddeb => [:debian_init, 'pkg/debian/DEBIAN/control'] do
  cd "pkg";
  system "dpkg-deb --build debian #{pkg_name}-#$VERSION.deb"
  cd "..";
end
